using System;
using System.Collections.Generic;
using System.Text;
using IGGCore;

namespace IGG
{
    /// <summary>
    /// The Boxscore formatter for the console
    /// </summary>
    public class ModeBoxscoreFormatter : ModeGameFormatterBase
    {
        #region IModeFormatter Members

        /// <summary>
        /// Format a complete boxscore for the console
        /// </summary>
        /// <param name="board"></param>
        public override void FormatMode(Scoreboard board)
        {
            if (board.SelectedGame == null)
            {
                return;
            }
            Boxscore boxscore = board.SelectedGame.Boxscore;
            Output.WriteLine(" " + ToolBox.GetFormattedDate(board.Date, false));
            Output.Write(" " + board.SelectedGame.Stadium.Name);
            Output.Write(", ");
            Output.WriteLine(ToolBox.GetVenue(board.SelectedGame.Stadium.VenueCode));
            Output.WriteLine("");

            if (board.SelectedGame.Linescore == null)
            {
                Output.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.AwayTeam.City + " " + board.SelectedGame.AwayTeam.Name, 22));
                Output.WriteLine("ab  r  h rbi   avg");
                Output.WriteLine();
                Output.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.HomeTeam.City + " " + board.SelectedGame.HomeTeam.Name, 22));
                Output.WriteLine("ab  r  h rbi   avg");
                Output.WriteLine();
                Output.WriteLine(" Pitchers");
                Output.WriteLine();
                Output.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.AwayTeam.City + " " + board.SelectedGame.AwayTeam.Name, 22));
                Output.WriteLine("IP  H  R  ER  BB  SO   ERA");
                Output.WriteLine();
                Output.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.HomeTeam.City + " " + board.SelectedGame.HomeTeam.Name, 22));
                Output.WriteLine("IP  H  R  ER  BB  SO   ERA");
                return;
            }

            WriteLinescore(board.SelectedGame);

            Output.WriteLine();
            Output.WriteLine();
            Output.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.AwayTeam.City + " " + board.SelectedGame.AwayTeam.Name, 22));
            Output.WriteLine("ab  r  h rbi   avg");
            foreach (Player player in board.SelectedGame.Boxscore.AwayBatting)
            {
                string prefix = "";
                if (String.IsNullOrEmpty(player.BO) == false)
                {
                    if (player.BO[2] == '1')
                    {
                        prefix = "- ";
                    }
                    else if (player.BO[2] == '2')
                    {
                        prefix = "-- ";
                    }
                    else if (player.BO[2] == '3')
                    {
                        prefix = "--- ";
                    }
                }
                Output.Write(" " + ToolBox.GetFormattedStringPadRight(prefix + player.LastName, 22));
                Output.Write(ToolBox.GetFormattedStringPadLeft(player.Ab.ToString(), 2));
                Output.Write(" ");
                Output.Write(ToolBox.GetFormattedStringPadLeft(player.R.ToString(), 2));
                Output.Write(" ");
                Output.Write(ToolBox.GetFormattedStringPadLeft(player.Hits.ToString(), 2));
                Output.Write(" ");
                Output.Write(ToolBox.GetFormattedStringPadLeft(player.RBI.ToString(), 3));
                Output.Write("   ");
                Output.Write(player.AVG.ToString());

                Output.WriteLine();
            }

            Output.WriteLine();
            if (board.SelectedGame.Boxscore.AwayBattingDetails != null)
            {
                Console.WriteLine(board.SelectedGame.Boxscore.AwayBattingDetails);
            }

            Output.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.HomeTeam.City + " " + board.SelectedGame.HomeTeam.Name, 22));
            Output.WriteLine("ab  r  h rbi   avg");
            foreach (Player player in board.SelectedGame.Boxscore.HomeBatting)
            {
                string prefix = "";
                if (String.IsNullOrEmpty(player.BO) == false)
                {
                    if (player.BO[2] == '1')
                    {
                        prefix = "- ";
                    }
                    else if (player.BO[2] == '2')
                    {
                        prefix = "-- ";
                    }
                    else if (player.BO[2] == '3')
                    {
                        prefix = "--- ";
                    }
                }
                Console.Write(" " + ToolBox.GetFormattedStringPadRight(prefix + player.LastName, 22));
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.Ab.ToString(), 2));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.R.ToString(), 2));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.Hits.ToString(), 2));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.RBI.ToString(), 3));
                Console.Write("   ");
                Console.Write(player.AVG.ToString());
                Console.WriteLine();
            }

            Console.WriteLine();
            if (board.SelectedGame.Boxscore.HomeBattingDetails != null)
            {
                Console.WriteLine(board.SelectedGame.Boxscore.HomeBattingDetails);
            }
            Console.WriteLine();

            Console.WriteLine(" Pitchers");
            Console.WriteLine();
            Console.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.AwayTeam.City + " " + board.SelectedGame.AwayTeam.Name, 22));
            Console.WriteLine("IP  H  R  ER  BB  SO   ERA");
            foreach (Player player in board.SelectedGame.Boxscore.AwayPitching)
            {
                if (player.Report == null)
                {
                    Console.Write(" " + ToolBox.GetFormattedStringPadRight(player.LastName, 21));
                }
                else
                {
                    Console.Write(" " + ToolBox.GetFormattedStringPadRight(player.LastName + " " + player.Report, 21));
                }
                Console.Write(player.IP);
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.Hits.ToString(), 2));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.R.ToString(), 2));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.EarnedRuns.ToString(), 3));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.BB.ToString(), 3));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.StrikeOuts.ToString(), 3));
                Console.Write("   ");
                Console.Write(player.ERA.ToString());
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.Write(" " + ToolBox.GetFormattedStringPadRight(board.SelectedGame.HomeTeam.City + " " + board.SelectedGame.HomeTeam.Name, 22));
            Console.WriteLine("IP  H  R  ER  BB  SO   ERA");
            foreach (Player player in board.SelectedGame.Boxscore.HomePitching)
            {
                if (player.Report == null)
                {
                    Console.Write(" " + ToolBox.GetFormattedStringPadRight(player.LastName, 21));
                }
                else
                {
                    Console.Write(" " + ToolBox.GetFormattedStringPadRight(player.LastName + " " + player.Report, 21));
                } 
                Console.Write(player.IP);
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.Hits.ToString(), 2));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.R.ToString(), 2));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.EarnedRuns.ToString(), 3));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.BB.ToString(), 3));
                Console.Write(" ");
                Console.Write(ToolBox.GetFormattedStringPadLeft(player.StrikeOuts.ToString(), 3));
                Console.Write("   ");
                Console.Write(player.ERA.ToString());
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine(boxscore.GameInfo);
            Console.CursorLeft = 0;
            Console.CursorTop = 0;
        }

        /// <summary>
        /// Format a linescore
        /// </summary>
        private void WriteLinescore(Game game)
        {
            int innCount = game.Status.Inning;

            // Away Team
            Console.Write(" " + ToolBox.GetFormattedStringPadRight(game.AwayTeam.City, 15));
            int remainingInning = 9;
            for (int i = 0; i < innCount; i++)
            {
                int pad = 3;
                if ((i + 1) % 3 == 0)
                {
                    pad = 4;
                }
                if (game.Linescore.Innings[i].Away > -1)
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.Innings[i].Away.ToString(), pad));
                }
                else
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight("0", pad));
                }
                remainingInning--;
            }

            Console.Write("-  ");
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Runs.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Hits.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetAwayLineScore().Errors.ToString(), 3));
            Console.WriteLine();

            // Home Team
            Console.Write(" " + ToolBox.GetFormattedStringPadRight(game.HomeTeam.City, 15));
            remainingInning = 9;
            for (int i = 0; i < innCount; i++)
            {
                int pad = 3;
                if ((i + 1) % 3 == 0)
                {
                    pad = 4;
                }
                if (game.Linescore.Innings[i].Home > -1)
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.Innings[i].Home.ToString(), pad));
                }
                else
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight(" ", pad));
                }
                remainingInning--;
            }

            Console.Write("-  ");
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Runs.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Hits.ToString(), 3));
            Console.Write(ToolBox.GetFormattedStringPadRight(game.Linescore.GetHomeLineScore().Errors.ToString(), 3));

        }

        #endregion
    }
}
